package com.i_art_engineer.meigong.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.i_art_engineer.meigong.AppConsts;
import com.i_art_engineer.meigong.AppContext;
import com.i_art_engineer.meigong.AppHttpClient;
import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.base.ActionBarActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

/**
 * 意见反馈界面
 */
public class FeedbackActivity extends ActionBarActivity {

    EditText et =null;
    Button btn_cancel,btn_submit;
    OnClickListener listener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_cancel:
                    finish();
                    break;
                case R.id.btn_submit:
                    String suggestion = et.getText().toString();
                    if(TextUtils.isEmpty(suggestion)){
                        Toast.makeText(FeedbackActivity.this,"请至少填写10个字符",Toast.LENGTH_SHORT).show();
                    }else{
                        AsyncHttpResponseHandler handler = new JsonHttpResponseHandler(){
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                Log.d("initAd", response.toString());
                                try {
                                    int code = response.getInt("code");
                                    String msg = response.getString("msg");
                                    if(code==200){
                                        if (msg==null)
                                        {
                                            msg = "";
                                        }
                                        Toast.makeText(FeedbackActivity.this,"感谢您的支持和反馈意见！"+msg,Toast.LENGTH_SHORT).show();
                                        finish();
                                    }
                                } catch (JSONException e) {
                                }
                            }

                            @Override
                            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                            }
                        };
                        RequestParams params = new RequestParams();
                        params.put("content",suggestion);
                        AppHttpClient.post(AppConsts.API_ACCOUNT_FEEBACK,params, handler);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        initActionBar(R.string.title_suggestion);
        et = (EditText)findViewById(R.id.et_suggestion);
        btn_cancel = (Button)findViewById(R.id.btn_cancel);
        btn_submit = (Button)findViewById(R.id.btn_submit);
        btn_cancel.setOnClickListener(listener);
        btn_submit.setOnClickListener(listener);
    }

}
